#!/bin/bash
#############################################################################
#此脚本用于在centos7服务器上安装oracle23c
#
##############################################################################
#安装依赖
yum install -y bc
yum install -y binutils
yum install -y compat-openssl10
yum install -y elfutils-libelf
yum install -y glibc
yum install -y glibc-devel
yum install -y ksh
yum install -y libaio
yum install -y libXrender
yum install -y libX11
yum install -y libXau
yum install -y libXi
yum install -y libXtst
yum install -y libgcc
yum install -y libnsl
yum install -y libstdc++
yum install -y libxcb
yum install -y libibverbs
yum install -y make
yum install -y policycoreutils
yum install -y policycoreutils-python-utils
yum install -y smartmontools
yum install -y sysstat
# Added by me.
yum install -y unixODBC

#修改系统参数
sudo cat <<'EOF'>/etc/sysctl.d/98-oracle.conf
fs.file-max = 6815744
kernel.sem = 250 32000 100 128
kernel.shmmni = 4096
kernel.shmall = 1073741824
kernel.shmmax = 4398046511104
kernel.panic_on_oops = 1
net.core.rmem_default = 262144
net.core.rmem_max = 4194304
net.core.wmem_default = 262144
net.core.wmem_max = 1048576
net.ipv4.conf.all.rp_filter = 2
net.ipv4.conf.default.rp_filter = 2
fs.aio-max-nr = 1048576
net.ipv4.ip_local_port_range = 9000 65500
EOF

/sbin/sysctl -p /etc/sysctl.d/98-oracle.conf

sudo cat <<'EOF'>/etc/security/limits.d/oracle-database-preinstall-21c.conf
oracle   soft   nofile    1024
oracle   hard   nofile    65536
oracle   soft   nproc    16384
oracle   hard   nproc    16384
oracle   soft   stack    10240
oracle   hard   stack    32768
oracle   hard   memlock    134217728
oracle   soft   memlock    134217728
oracle   soft   data    unlimited
oracle   hard   data    unlimited
EOF

sysctl -p &>  /dev/null


if  grep -q oinstall /etc/group ; then
    echo "group oinstall already exist"
else
    groupadd -g 54321 oinstall
fi
if  grep -q dba /etc/group ; then
    echo "group dba already exist"
else
    groupadd -g 54322 dba
fi
if  grep -q oper /etc/group ; then
    echo "group oper already exist"
else
    groupadd -g 54323 oper 
fi

if  grep -q oracle /etc/passwd ; then
    echo "user oracle already exist"
else
    #add user "oracle"
    useradd -u 54321 -g oinstall -G dba,oper oracle
fi
#Set the password for the "oracle" user.
echo "oracle" | passwd --stdin oracle

chown -R oracle:oinstall /home/oracle
chmod -R 775 /home/oracle

ORACLE_SID=ORCL
ORACLE_BASE=/u01/app/oracle
ORACLE_HOME=$ORACLE_BASE/product/21.0.0/dbhome_1
ORA_INVENTORY=/u01/app/oraInventory
DATA_DIR=/u02/oradata
ADUMP_DIR=$ORACLE_BASE/admin/$ORACLE_SID/adump

echo "ORACLE_SID=$ORACLE_SID"
echo "ORACLE_BASE=$ORACLE_BASE"
echo "ORACLE_HOME=$ORACLE_HOME"
echo "ORA_INVENTORY=$ORA_INVENTORY"
echo "DATA_DIR=$DATA_DIR"
echo "ADUMP_DIR=$ADUMP_DIR"

if [ ! -d $ORACLE_HOME ]; then
mkdir -p $ORACLE_HOME
else
echo "dir $ORACLE_HOME already exist"
fi

chown -R oracle:oinstall $ORACLE_BASE
chmod -R 775 $ORACLE_BASE
chown -R oracle:oinstall $ORACLE_HOME
chmod -R 775 $ORACLE_HOME

if [ ! -d $ORA_INVENTORY ]; then
echo "mkdir -p $ORA_INVENTORY"
mkdir -p $ORA_INVENTORY
else
echo "dir $ORA_INVENTORY already exist"
fi
chown -R oracle:oinstall $ORA_INVENTORY
chmod -R 775 $ORA_INVENTORY

if [ ! -d $DATA_DIR ]; then
echo "mkdir -p $DATA_DIR"
mkdir -p $DATA_DIR
else
echo "dir $DATA_DIR already exist"
fi
chown -R oracle:oinstall $DATA_DIR
chmod -R 775 $DATA_DIR

if [ ! -d $ADUMP_DIR ]; then
echo "mkdir -p $ADUMP_DIR"
mkdir -p $ADUMP_DIR
else
echo "dir $ADUMP_DIR already exist"
fi
chown -R oracle:oinstall $ADUMP_DIR
chmod -R 775 $ADUMP_DIR


local_host="`hostname --fqdn`"

mkdir -p /home/oracle/scripts
cat > /home/oracle/scripts/setEnv.sh <<EOF
# Oracle Settings
export TMP=/tmp
export TMPDIR=\$TMP

export ORACLE_HOSTNAME=$local_host
export ORACLE_UNQNAME=ORCL
export ORACLE_BASE=$ORACLE_BASE
export ORACLE_HOME=$ORACLE_HOME
export ORA_INVENTORY=$ORA_INVENTORY
export ORACLE_SID=ORCL
export PDB_NAME=PDBORCL
export DATA_DIR=$DATA_DIR

export PATH=/usr/sbin:/usr/local/bin:\$PATH
export PATH=\$ORACLE_HOME/bin:\$PATH

export LD_LIBRARY_PATH=\$ORACLE_HOME/lib:/lib:/usr/lib
export CLASSPATH=\$ORACLE_HOME/jlib:\$ORACLE_HOME/rdbms/jlib

export NLS_LANG=AMERICAN_AMERICA.AL32UTF8
export NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'


EOF


echo ". /home/oracle/scripts/setEnv.sh" > /home/oracle/.bash_profile

cat > /home/oracle/scripts/start_all.sh <<EOF
#!/bin/bash
. /home/oracle/scripts/setEnv.sh

export ORAENV_ASK=NO
. oraenv
export ORAENV_ASK=YES

dbstart \$ORACLE_HOME
EOF


cat > /home/oracle/scripts/stop_all.sh <<EOF
#!/bin/bash
. /home/oracle/scripts/setEnv.sh

export ORAENV_ASK=NO
. oraenv
export ORAENV_ASK=YES

dbshut \$ORACLE_HOME
EOF

chown -R oracle:oinstall /home/oracle/scripts
chmod u+x /home/oracle/scripts/*.sh


cd $ORACLE_HOME
unzip -oq /home/oracle-install-backup/download/LINUX.X64_213000_db_home.zip 


# Preparing Response Files
cp ${ORACLE_HOME}/install/response/db_install.rsp ${ORACLE_HOME}/install/response/db_install.rsp.bak

sed -i "s/oracle.install.option=/oracle.install.option=INSTALL_DB_SWONLY/g" ${ORACLE_HOME}/install/response/db_install.rsp &>  /dev/null
sed -i "s/UNIX_GROUP_NAME=/UNIX_GROUP_NAME=oinstall/g" ${ORACLE_HOME}/install/response/db_install.rsp &>  /dev/null



echo "ORACLE_BASE=$ORACLE_BASE"
echo "ORACLE_HOME=$ORACLE_HOME"
echo "ORA_INVENTORY=$ORA_INVENTORY"
echo "ADUMP_DIR=$ADUMP_DIR"
echo "DATA_DIR=$DATA_DIR"


chown -R oracle:oinstall $ORACLE_HOME
chmod -R 775 $ORACLE_HOME

# 使用Silent mode安装数据库软件.
su - oracle -c '${ORACLE_HOME}/runInstaller -ignorePrereq -waitforcompletion -silent                        \
    -responseFile ${ORACLE_HOME}/install/response/db_install.rsp               \
    oracle.install.option=INSTALL_DB_SWONLY                                    \
    ORACLE_HOSTNAME=${ORACLE_HOSTNAME}                                         \
    UNIX_GROUP_NAME=oinstall                                                   \
    INVENTORY_LOCATION=${ORA_INVENTORY}                                        \
    SELECTED_LANGUAGES=en,en_GB                                                \
    ORACLE_HOME=${ORACLE_HOME}                                                 \
    ORACLE_BASE=${ORACLE_BASE}                                                 \
    oracle.install.db.InstallEdition=EE                                        \
    oracle.install.db.OSDBA_GROUP=dba                                          \
    oracle.install.db.OSBACKUPDBA_GROUP=dba                                    \
    oracle.install.db.OSDGDBA_GROUP=dba                                        \
    oracle.install.db.OSKMDBA_GROUP=dba                                        \
    oracle.install.db.OSRACDBA_GROUP=dba                                       \
    SECURITY_UPDATES_VIA_MYORACLESUPPORT=false                                 \
    DECLINE_SECURITY_UPDATES=true
'

sh /u01/app/oraInventory/orainstRoot.sh
sh /u01/app/oracle/product/21.0.0/dbhome_1/root.sh

#开启监听
su - oracle -c 'lsnrctl start'
su - oracle -c 'lsnrctl status'

# 安装数据库并创建数据库实例 sh install.sh createDatabase
# 仅安装数据库软件 sh install.sh
if [ -n "$1" ] ;then
  if [ "$1" -eq "createDatabase" ]; then
    echo "安装数据库并创建数据库实例"
    #创建第一个数据库实例
    su - oracle -c 'dbca -silent -createDatabase                                                   \
        -templateName General_Purpose.dbc                                         \
        -gdbname ${ORACLE_SID} -sid  ${ORACLE_SID} -responseFile NO_VALUE         \
        -characterSet AL32UTF8                                                    \
        -sysPassword Windows-789                                                 \
        -systemPassword Windows-789                                              \
        -createAsContainerDatabase true                                           \
        -numberOfPDBs 1                                                           \
        -pdbName ${PDB_NAME}                                                      \
        -pdbAdminPassword PdbPassword1                                            \
        -databaseType MULTIPURPOSE                                                \
        -memoryMgmtType auto_sga                                                  \
        -totalMemory 2000                                                         \
        -storageType FS                                                           \
        -datafileDestination "${DATA_DIR}"                                        \
        -redoLogFileSize 50                                                       \
        -emConfiguration NONE                                                     \
        -ignorePreReqs
    '
  fi
else 
  echo "仅安装数据库软件"
fi